/**
 * 钉钉工具
 * Created by zhangluxin on 2017/2/18.
 */
var DDUtil = {};

/**
 * 是否安装
 * @return {Boolean}
 * @public
 */
DDUtil.isInstalled = function () {
    if (cc.sys.os == cc.sys.OS_IOS) {
        return jsb.reflection.callStaticMethod('DDUtil', 'isDDAppInstalled');
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        return jsb.reflection.callStaticMethod(packageUri + '/utils/DDUtil', 'isInstalled', '()Z');
    }
};

DDUtil.registerAppByAppId = function (app_id) {
    if (cc.sys.os === cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            'DDUtil',
            'registerAppByAppId:',
            app_id
        );
    } else if (cc.sys.os === cc.sys.OS_ANDROID) {
    }
};

/**
 * 分享文本到钉钉
 * @param {String} text 文本内容
 * @param {String} [sceneType] 0
 * @param {String} [transaction] 0
 */
DDUtil.shareText = function (text, sceneType, transaction) {
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            'DDUtil',
            'shareText:',
            text
        );
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(packageUri + '/utils/DDUtil',
            'shareText',
            '(Ljava/lang/String;)V',
            text
        );
    }
};

/**
 * 分享url到钉钉
 * @param {String} url 地址
 * @param {String} title 标题
 * @param {String} description 内容
 * @param {Boolean || Number} sceneType 分享类型
 * @param {String} transaction 单号(时间戳)
 */
DDUtil.shareUrl = function (url, title, description, sceneType, transaction) {
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            'DDUtil',
            'shareUrl:title:description:sceneType:',
            url,
            title,
            description,
            (sceneType ? 1 : 0)
        );
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(
            packageUri + '/utils/DDUtil',
            'shareUrl',
            '(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZLjava/lang/String;)V',
            url,
            title,
            description,
            !!sceneType,
            transaction
        );
    }
};
DDUtil.shareUrlWithIcon = function (url, title, description, sceneType, transaction, iconPath) {
    if (!iconPath) {

        var path = jsb.fileUtils.getWritablePath();
        path += (path.charAt(path.length - 1) == '/' ? '' : '/');
        path += (cc.sys.os == cc.sys.OS_IOS ? 'storage/' : '');
        iconPath = path + 'res/icon.png';

        //iconPath = jsb.fileUtils.getWritablePath() + "res/icon.png";
        //iconPath = jsb.fileUtils.getWritablePath() + (cc.sys.os == cc.sys.OS_IOS ? "/storage/" : "/")+'res/icon.png';
    }
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            'DDUtil',
            'shareUrl:title:description:sceneType:path:',
            url,
            title,
            description,
            (sceneType ? 1 : 0),

            iconPath
        );
    } else {
        jsb.reflection.callStaticMethod(
            packageUri + '/utils/DDUtil',
            'shareUrlWithIcon',
            '(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZLjava/lang/String;Ljava/lang/String;)V',
            url,
            title,
            description,
            (sceneType ? true : false),
            transaction,
            iconPath
        );
    }
};

/**
 * 分享图片到钉钉
 * @param node 截图的node
 */
DDUtil.sharePic = function (node, depthStencilFormat) {
    var winSize = cc.director.getWinSize();
    var texture = new cc.RenderTexture(winSize.width, winSize.height, null, depthStencilFormat);
    if (!texture)
        return;

    texture.retain();
    texture.setAnchorPoint(0, 0);
    texture.begin();
    node.visit();
    texture.end();
    var time = timestamp2time(Math.round((new Date()).valueOf() / 1000));
    time = time.replace(/[\s:-]+/g, '_');
    var namePNG = 'ss-' + time + '.png';
    var nameJPG = 'ss-' + time + '.jpg';

    if (cc.sys.os == cc.sys.OS_IOS) {
        texture.saveToFile(namePNG, cc.IMAGE_FORMAT_PNG, true, function () {
            texture.release();
            jsb.reflection.callStaticMethod(
                'DDUtil',
                'sharePic:imageName:sceneType:',
                jsb.fileUtils.getWritablePath(),
                namePNG,
                0
            );
        });

    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        texture.saveToFile(nameJPG, cc.IMAGE_FORMAT_JPEG, false, function () {
            texture.release();
            jsb.reflection.callStaticMethod(
                packageUri + '/utils/DDUtil',
                'sharePic',
                '(Ljava/lang/String;Z)V',
                nameJPG,
                false
            );
        });
    }
};
DDUtil.shareCapture = function (node) {
    DDUtil.sharePic(node,0x88F0);
};